package com.model.action.vacate;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.model.action.BaseAction;
import com.model.service.admin.AdminService;
import com.model.service.student.StudentService;
import com.model.service.teacher.TeacherService;
import com.model.service.vacate.VacateService;
import com.model.util.PageList;
import com.model.util.StringUtil;
import com.model.util.TimeUtil;
import com.model.vo.Academy;
import com.model.vo.Admin;
import com.model.vo.ChartVo;
import com.model.vo.Student;
import com.model.vo.Teacher;
import com.model.vo.Vacate;

@SuppressWarnings("serial")
public class VacateAction extends BaseAction implements SessionAware{

		/*实体参数 声明
		 **********************************************/
      	Map session;
	    Vacate vacate;   //实体类
	    String pass;
		@Autowired
		VacateService vacateService;  //业务层
		Student student;   //实体类
		@Autowired
		StudentService studentService;  //业务层
		String resultView;   //视图页面
		PageList<Vacate> pageList;
		 private List<File> file;
		 private List<String> fileFileName;
		 private List<String> fileContentType;
		/*相关方法
		 * ********************************************/
	    @Override
	    public String execute() throws Exception {
	    	if(student==null){
	    		student = new Student();
	    		student.setId((Integer) session.get("nameId"));
	    	}
	    	student = studentService.QueryById(Student.class, student.getId());
	    	resultView="/admin/vacate/addVacate.jsp";
	    	return INPUT;
	    }
		/**
		 * 保存
		 * @return
		 */
		public String save(){
			
			if(vacate!=null){
				String uploadFileName  =new TimeUtil().getTimeLong()+StringUtil.getExtention(fileFileName.get(0));
				String path = ServletActionContext.getServletContext().getRealPath("/file");
				try {
				if (file != null) {
		            File savefile = new File(new File(path),uploadFileName);
		            if (!savefile.getParentFile().exists())
		                savefile.getParentFile().mkdirs();
		            FileUtils.copyFile(file.get(0), savefile);
		            vacate.setFilePath(uploadFileName);
		            vacate.setCheckTeacherState("0");
		            vacate.setCheckLeaderState("0");
		            vacate.setCheckStuWorkState("0");
		            vacateService.save(vacate);
				}
				} catch (Exception e) {
					e.printStackTrace();
					if(student==null){
			    		student = new Student();	
			    		student.setId(9);
			    	}
					student = studentService.QueryById(Student.class, student.getId());
		         System.out.println("失败");
		         return INPUT;
				}	
		
				
			}
			 resultView ="/admin/vacate_queryList.action";
			return "update";
		}
		/**
		 * 更新
		 * @return
		 */
	    public String update(){
	    	if(vacate!=null){
	    		String role = (String)session.get("role");
	    		if(pass!=null){
	    			System.out.println(vacate);
	    		if(role.equals("12")){
	    			if(pass.equals("0")){
		    			vacate.setCheckTeacherState("1");
	    			}else if(pass.equals("1")){
	    				vacate.setCheckTeacherState("2");
	    			}
	    		}else if(role.equals("22")){
	    			if(pass.equals("0")){
		    			vacate.setCheckLeaderState("1");
	    			}else if(pass.equals("1")){
	    				vacate.setCheckLeaderState("2");
	    			}
	    		}else if(role.equals("32")){
	    			if(pass.equals("0")){
		    			vacate.setCheckStuWorkState("1");
	    			}else if(pass.equals("1")){
	    				vacate.setCheckStuWorkState("2");
	    			}
	    		}
	    	}
	    		vacateService.update(vacate);
			}
	    	 resultView ="/admin/vacate_queryList.action";
			return "update";
		}
	    /**
	     * 删除
	     * @return
	     */
	   public String delete(){
		   if(vacate!=null){
			   vacateService.delete(vacate);
			}
		   resultView ="/admin/vacate_queryList.action";
			return "update";
		}
	   /**
	    * 分页查询
	    * @return
	    */
		public String queryList(){
		
	    	if(pageList==null){
	    		pageList = new PageList<Vacate>();
	    		if(session.get("role").equals("1")){
	    			pageList.setStuName((String)session.get("name"));
	    		}
	    	}
	    	
	    	Object[] objects =null;
	    	String hql = "from Vacate "; 
	    	 if(pageList.getStuName()!=null){
	    			 objects = new Object[1];
	    			hql += "where   stuName like ?";
	    			objects[0] = "%"+pageList.getStuName()+"%";
	    		}else if(pageList.getGradeClass()!=null){
	    			hql += "where   gradeClass like ?";
	    			 objects = new Object[1];
	    			objects[0] = "%"+pageList.getGradeClass()+"%";
	    		}
	         
	    	    pageList.setPageSize(15);
	    		int allrow = vacateService.getAllRowCount(hql, objects);
	    		pageList.setRowCount(allrow);
	    		pageList.setDataList(vacateService.queryForPage(
	    		pageList.getStartOfPage(),
	    		pageList.getEndRow(),hql,objects));
			return SUCCESS;
		}
	    
	    /**
	     * 通过Id查询
	     * @return
	     */
	   public String queryById(){
		   if(vacate==null){
			   vacate = new Vacate();
			}	
		   vacate = vacateService.QueryById(Vacate.class,vacate.getId());
		   if(!session.get("role").equals("1")){
	    		resultView ="/admin/vacate/vacateDetails.jsp";
	    		return INPUT;
	    	}else{
	    		return "edit";
	    	}
			
			
		}
	   /***
	    * json查询
	    */
	   public void queryJsonList(){
		   if(pageList==null){
	    		pageList = new PageList<Vacate>();	
	    	}
	    	    	Object[] objects =null;
	    	    	String hql = "select * from vacate where 1=1 ";
	    	    	 if(pageList.getStuName()!=null){
	    	    			hql += "and  stuName = ?";
	    	    			objects = new Object[]{pageList.getStuName()};
	    	    	 }
	    	    	 if(pageList.getGradeClass()!=null){
    	    			hql += "and  gradeClass = ?";
    	    			objects = new Object[]{pageList.getGradeClass()};
    	    	 }
	    	    if(pageList.getStuName()!=null&&pageList.getGradeClass()!=null){
	    	    	objects = new Object[]{pageList.getStuName(),pageList.getGradeClass()};
	    	    }
	    	    pageList.setDataList(vacateService.QueryBySqlList(hql,objects,Vacate.class));
	    	    resultJson();
	   
	   }
		/**
		 * 
		 * @param json
		 */
		private void resultJson() {
			try {
				HttpServletResponse response = ServletActionContext.getResponse();
				response.setContentType("text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				Gson g  = new Gson();
				String json = g.toJson(pageList);
		        PrintWriter pw = response.getWriter();
		        pw.print(json);
		        pw.flush();
		        pw.close();
			} catch (Exception e) {
				e.printStackTrace();
			}	

		}
		
	   /*相关参数注入
	  	 *********************************************/
		
		public String getResultView() {
			return resultView;
		}
	
		public Vacate getVacate() {
			return vacate;
		}
		public void setVacate(Vacate vacate) {
			this.vacate = vacate;
		}
		public VacateService getVacateService() {
			return vacateService;
		}
		public void setVacateService(VacateService vacateService) {
			this.vacateService = vacateService;
		}
	
		public void setResultView(String resultView) {
			this.resultView = resultView;
		}
		public PageList<Vacate> getPageList() {
			return pageList;
		}
		public void setPageList(PageList<Vacate> pageList) {
			this.pageList = pageList;
		}
		public Student getStudent() {
			return student;
		}
		public void setStudent(Student student) {
			this.student = student;
		}
		public StudentService getStudentService() {
			return studentService;
		}
		public void setStudentService(StudentService studentService) {
			this.studentService = studentService;
		}
		public List<File> getFile() {
			return file;
		}
		public void setFile(List<File> file) {
			this.file = file;
		}
		public List<String> getFileFileName() {
			return fileFileName;
		}
		public void setFileFileName(List<String> fileFileName) {
			this.fileFileName = fileFileName;
		}
		public List<String> getFileContentType() {
			return fileContentType;
		}
		public void setFileContentType(List<String> fileContentType) {
			this.fileContentType = fileContentType;
		}
		public Map getSession() {
			return session;
		}
		public void setSession(Map session) {
			this.session = session;
		}
		public String getPass() {
			return pass;
		}
		public void setPass(String pass) {
			this.pass = pass;
		}
			
		
			    
	}
